/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProfilerGUI.java
 *
 * Created on 31-Oct-2010, 17:21:05
 */
package view;

import agents.ProfilerAgent;
import jade.core.AID;
import java.util.ArrayList;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import model.Artifact;
import model.User;

/**
 *
 * @author CUNEYT
 *
 * This class represents the GUI of the Profiler agent and serves as the interface
 * to perform the search operations over the museums/galleries or tour guides. It also
 * serves as the interface for listing all the available services which are registered
 * with the DF.
 *
 */
public class ProfilerGUI extends javax.swing.JFrame {

    private class ServiceListSelectionListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {

            AID elem = (AID) serviceList.getSelectedValue();
            if (elem != null) {
                searchButton.setVisible(true);

                if (elem.getLocalName().contains("tour")) {
                    searchByComboBox.setVisible(true);
                    searchByLabel.setVisible(true);
                    searchCriteriaLabel.setVisible(true);
                    criteriaTextField.setVisible(true);
                    criteriaTextField.setText("Anything related to the search criteria");

                } else {
                    searchCriteriaLabel.setVisible(true);
                    criteriaTextField.setVisible(true);
                    searchByComboBox.setVisible(false);
                    searchByLabel.setVisible(false);
                    criteriaTextField.setText("Enter artifact ID");
                }
            } else {
                searchButton.setVisible(false);
                searchByComboBox.setVisible(false);
                searchByLabel.setVisible(false);
                searchCriteriaLabel.setVisible(false);
                criteriaTextField.setVisible(false);
            }

        }
    }
    private ProfilerAgent profiler;
    private ArrayList<AID> services;

    /** Creates new form ProfilerGUI */
    public ProfilerGUI(ProfilerAgent pa) {
        initComponents();
        this.profiler = pa;
        this.services = new ArrayList<AID>();
        this.serviceList.setModel(new SMListModel(services));
        this.serviceList.addListSelectionListener(new ServiceListSelectionListener());
    }

    public void setResult(String param) {
        resultTextPane.setText(param);
    }

    public JList getServiceList() {
        return serviceList;
    }

    public ArrayList<AID> getServices() {
        return services;
    }

    public void setServices(ArrayList<AID> services) {
        this.services = services;
    }

    public User createUser() {
        User usr = null;
        int agee = 0;
        String name = JOptionPane.showInputDialog(this, "Name:", "User Profile Information", JOptionPane.QUESTION_MESSAGE);
        while (agee == 0) {
            String age = JOptionPane.showInputDialog(this, "Age:", "User Profile Information", JOptionPane.QUESTION_MESSAGE);
            try {
                agee = Integer.parseInt(age);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(this, nfe);
            }
        }
        String occupation = JOptionPane.showInputDialog(this, "Occupation:", "User Profile Information", JOptionPane.QUESTION_MESSAGE);
        String gender = JOptionPane.showInputDialog(this, "Gender:", "User Profile Information", JOptionPane.QUESTION_MESSAGE);
        String ints = JOptionPane.showInputDialog(this, "Interests(separate, with, comma):", "User Profile Information", JOptionPane.QUESTION_MESSAGE);
        String[] intr = ints.split(",");



        ArrayList<String> interests = new ArrayList<String>();
        for (int i = 0; i < intr.length; i++) {
            String string = intr[i];
            interests.add(string);
        }
        usr = new User(name, agee, occupation, gender, interests);
        return usr;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        serviceScrollPane = new javax.swing.JScrollPane();
        serviceList = new javax.swing.JList();
        updateServicesButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        searchCriteriaLabel = new javax.swing.JLabel();
        criteriaTextField = new javax.swing.JTextField();
        searchByLabel = new javax.swing.JLabel();
        searchByComboBox = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        resultScrollPane = new javax.swing.JScrollPane();
        resultTextPane = new javax.swing.JTextPane();
        searchButton = new javax.swing.JButton();
        availableServicesLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Profiler Agent");

        serviceList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        serviceList.setCellRenderer(new SMCellRenderer());
        serviceList.setVisibleRowCount(-1);
        serviceScrollPane.setViewportView(serviceList);

        updateServicesButton.setText("UPDATE");
        updateServicesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateServicesButtonActionPerformed(evt);
            }
        });

        searchCriteriaLabel.setText("Search Criteria");
        searchCriteriaLabel.setVisible(false);

        criteriaTextField.setVisible(false);

        searchByLabel.setText("Search by");
        searchByLabel.setVisible(false);

        searchByComboBox.addItem(Artifact.CATEGORY);
        searchByComboBox.addItem(Artifact.NAME);
        searchByComboBox.addItem(Artifact.CREATOR);
        searchByComboBox.addItem(Artifact.GENRE);
        searchByComboBox.setVisible(false);

        resultTextPane.setEditable(false);
        resultScrollPane.setViewportView(resultTextPane);

        searchButton.setText("SEARCH");
        searchButton.setVisible(false);
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        availableServicesLabel.setText("AVAILABLE SERVICES");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(resultScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                    .addComponent(serviceScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(availableServicesLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 225, Short.MAX_VALUE)
                        .addComponent(updateServicesButton))
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(searchCriteriaLabel)
                            .addComponent(searchByLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(searchByComboBox, 0, 246, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchButton))
                            .addComponent(criteriaTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serviceScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(updateServicesButton)
                    .addComponent(availableServicesLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchCriteriaLabel)
                    .addComponent(criteriaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchByLabel)
                    .addComponent(searchByComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(resultScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void updateServicesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateServicesButtonActionPerformed

        profiler.update();

    }//GEN-LAST:event_updateServicesButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        String sby = null;
        if (searchByComboBox.isVisible()) {
            sby = searchByComboBox.getSelectedItem().toString();
        }
        profiler.search((AID) serviceList.getSelectedValue(), criteriaTextField.getText(), sby);
    }//GEN-LAST:event_searchButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel availableServicesLabel;
    private javax.swing.JTextField criteriaTextField;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JScrollPane resultScrollPane;
    private javax.swing.JTextPane resultTextPane;
    private javax.swing.JButton searchButton;
    private javax.swing.JComboBox searchByComboBox;
    private javax.swing.JLabel searchByLabel;
    private javax.swing.JLabel searchCriteriaLabel;
    private javax.swing.JList serviceList;
    private javax.swing.JScrollPane serviceScrollPane;
    private javax.swing.JButton updateServicesButton;
    // End of variables declaration//GEN-END:variables
}
